﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace InventoryPro.DAL
{
    public class OrderDAO
    {
        InvEntities context ;
        public OrderDAO() 
        {
             
        context = new InvEntities(); 
        }

        public List<Order> GetAllOrders()
        {

            
            var query = from o in context.Orders                       
                        select o;
 
            //var queryResult = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            //             context.Dispose();

           return  query.ToList<Order>();
            
         }

        public Order  GetOrderbyID(int ID)

        {
            Order order = null;
            var query = from o in context.Orders
                        where o.OrderID == ID
                        select o;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Order o in Q)
            {
                order = o;                
            }

            if(order != null)
            context.Detach(order);
            return order;
        }


        public void CreateOrder(Order order)

        {
            context.AddToOrders(order);
            context.SaveChanges();

        }


        public void UpdateOrder(Order order)

        {

            context.Attach(order);
             context.SaveChanges();
        }


        public void DeleteOrder(int ID)
        {
            Order order = null;
            var query = from o in context.Orders
                        where o.OrderID == ID
                        select o;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Order o in Q)
            {
                order = o;                
            }

            context.DeleteObject(order);
            
        }

        public List<Order> Search(SearchOrder searchOrder)
        {

            IQueryable<Order> orderQuery = Queryable.AsQueryable(context.Orders);

            if((searchOrder.CustomerId != null) && (searchOrder.CustomerId != ""))
            orderQuery = orderQuery.Where(order => order.CustomerID == searchOrder.CustomerId);

            if(searchOrder.Id != 0)
            orderQuery = orderQuery.Where(order => order.OrderID == searchOrder.Id);

            if (searchOrder.EmployeeId != 0)
                orderQuery = orderQuery.Where(order => order.EmployeeID == searchOrder.EmployeeId);

            if(searchOrder.OrderDate != null)
                orderQuery = orderQuery.Where(order => order.OrderDate == searchOrder.OrderDate);

            if (searchOrder.RequiredDate != null)
                orderQuery = orderQuery.Where(order => order.RequiredDate == searchOrder.RequiredDate);

            if (searchOrder.OrderItemId != 0)
                orderQuery = orderQuery.Where(order => order.Order_Details.Any(item => item.ItemId == searchOrder.OrderItemId));



            //IQueryable<Order> orderQuery = context.Orders.Where(order => order.OrderID == searchOrder.Id);
            // orderQuery = orderQuery.Where(order => order.CustomerID==searchOrder.CustomerId);
            
            return orderQuery.ToList<Order>();
            //orderQuery.Where(order=>order.Order_Details.AsQueryable<Order_Detail>().Where(item=>item.

            //var query = from o in context.Orders
            //            select o;

            ////var queryResult = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            ////             context.Dispose();

            //return query.ToList<Order>();

        }



        }
}




